﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Runtime.InteropServices.ComTypes;

namespace 数据库链接
{
    class Program
    {
        static void Main(string[] args)
        {
            #region 连接数据库
            //写数据库连接字符串
            string conString = "server=DESKTOP-6H8UH6Q;database=StudentData;uid=sa;pwd=xhq053100";
            //连接数据库
            SqlConnection sqlConnection = new SqlConnection(conString);
            //打开数据库
            sqlConnection.Open();
            //Sql语句
            var sqlcommandString = "select * from Student";
            //执行Sql语句
            SqlCommand sqlCommand = new SqlCommand(sqlcommandString, sqlConnection);
            //阅读数据库表
            SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
            //遍历
            while (sqlDataReader.Read())
            {
                Console.WriteLine(sqlDataReader["Id"]);
            }
            #endregion
        }
    }
}
